﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace EwbDesigner.NET
{
    class Licensing : License
    {
        public override void Dispose()
        {
            //
        }

        public override string LicenseKey
        {
            get
            {
                return Guid.NewGuid().ToString();
            }
        }

        public void TestLicense()
        {
            //static void Main() {
            try
            {
                // Attempt to validate the license 
                LicenseManager.Validate(typeof(Program));

                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());
            }
            catch (Exception exception)
            {
                MessageBox.Show("An unhandled application exception occurred." + Environment.NewLine + Environment.NewLine + exception.ToString());
            }
        }

        public class LicensingProvider : LicenseProvider
        {

            public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
            {
                // Do some logic to go figure out if this type or instance 
                // is licensed. This can be implemented however you want. 
                bool licenseIsValid = false;

                licenseIsValid = true;

                // If license check isn't successful: 
                if (licenseIsValid)
                {
                    throw new LicenseException(type, instance, "Invalid license.");
                }
                else
                {
                    return new Licensing();
                }
            }

           /*
           public override License GetLicense(LicenseContext context, Type type, 
                object instance, bool allowExceptions) 
           { 
                // Get the path of the would-be license file 
                string path = Path.GetFullPath("."); 
                string licenseFile = Path.Combine(path, "MyCustomLicense.lic"); 
          
                // See if the file exists, if it doesn't - exception out 
                if (!File.Exists(licenseFile)) 
                { 
                    throw new LicenseException(type, instance, 
                        "A licensing error occurred. The MyCustomLicense licensing file was not found in the following location: '" 
                        + licenseFile + "'. Please place the license file in this location and try the application again."); 
                } 
          
                // Read the contents of the file, if it'key null or empty - exception out 
                string fileContents = File.ReadAllText(licenseFile); 
                if ( string.IsNullOrEmpty(fileContents)) 
                { 
                    throw new LicenseException(type, instance, 
                        "A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '" 
                        + licenseFile + "'. However, the file is empty. Please place the license file in this location and try the application again."); 
                } 
          
                // Deserialize this XML file into a real data structure. If there was a problem deserializing - exception out 
                XmlDomainObjects.License license = null; 
                try 
                { 
                    license = 
                        LicenseUtilities.Deserialize<XmlDomainObjects.License>(fileContents, "license"); 
                } 
                catch (LicenseUtilityException exception) 
                { 
                    throw new LicenseException(type, instance, 
                        "A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '" 
                        + licenseFile + "'. However, the file does not appear to be a valid license file. Please place the license file in this location and try the application again."); 
                } 
          
                // Generate a hash from the domain properties 
                string hash = LicenseUtilities.GetHashForLicense(license); 
          
                // See if the generated hash matches what was in the file, if not - exception out 
                if (!license.Hash.Equals(hash, StringComparison.InvariantCulture)) 
                { 
                    throw new LicenseException(type, instance, 
                        "A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '" 
                        + licenseFile + "'. However, it has been modified and can no longer be verified. Please replace this file with the correct license file or contact support for a new license file."); 
                } 
          
                return license; 
           }
            
          */

        }


    }
}
